package com.zxkj.commonpatrol.adapt;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.daimajia.swipe.SwipeLayout;
import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.GravityEnum;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.zxkj.commonpatrol.R;
import com.zxkj.commonpatrol.model.entity.SubAlarmLogHistory;
import com.zxkj.commonpatrol.model.response.DevEvtQuestPatrolHistoryResponse;
import com.zxkj.commonpatrol.ui.ShowMediaActivity;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class SubHistoryQuestAdapt extends BaseQuickAdapter<SubAlarmLogHistory, BaseViewHolder> {
    public SubHistoryQuestAdapt(int layoutResId, @Nullable List<SubAlarmLogHistory> data) {
        super(layoutResId, data);
    }
    public MyCheckPointListCallBack mDataCallBack;
    public OnCallPhoneCallBack onCallPhoneCallBack;
    //数据回调接口
    public interface MyCheckPointListCallBack<T> {
        void onDataSuccessCallBack(T t);
        void onDataFailCallBack(T t, T t1);
    }
    public MyUnCheckPointListCallBack mUnDataCallBack;



    public void setmUnDataCallBack(MyUnCheckPointListCallBack<String> mUnDataCallBack) {
        this.mUnDataCallBack = mUnDataCallBack;
    }

    //数据回调接口
    public interface MyUnCheckPointListCallBack<T> {
        void onDataSuccessCallBack(T t, T t1);
        void onDataFailCallBack(T t, T t1);
    }
    //电话回调
    public interface OnCallPhoneCallBack{
        void onCallPhone(String number);
    }

    public void setOnCallPhoneCallBack(OnCallPhoneCallBack onCallPhoneCallBack) {
        this.onCallPhoneCallBack = onCallPhoneCallBack;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, @Nullable SubAlarmLogHistory historyQuestEntity) {
        SwipeLayout swipeLayout= baseViewHolder.itemView.findViewById(R.id.swipe);
        swipeLayout.setClickToClose(true);
        TextView viewById = baseViewHolder.itemView.findViewById(R.id.tv_upcoming_name);
        ImageView viewById1 = baseViewHolder.itemView.findViewById(R.id.tv_num);
        LayoutInflater inflater = LayoutInflater.from(getContext());

        LinearLayout llAddView = baseViewHolder.itemView.findViewById(R.id.ll_add_view);
        llAddView.removeAllViews();
        if (historyQuestEntity.getType()==2){
            swipeLayout.setSwipeEnabled(false);
            viewById.setTextColor(getContext().getColor(R.color.red));
        }else{
            if (historyQuestEntity.getResultType()==0){
                swipeLayout.setSwipeEnabled(false);
                viewById.setTextColor(getContext().getColor(R.color.red));
            }else{
                swipeLayout.setSwipeEnabled(false);
                viewById.setTextColor(getContext().getColor(R.color.green));
            }
        }
        if (historyQuestEntity.getQItemOptionList()!=null){
            if (historyQuestEntity.getQItemOptionList().size()>0){
                for (int i = 0; i < historyQuestEntity.getQItemOptionList().size(); i++) {
//                          textView12.setText(checkPointListEntity.getDevices().getDevPropertys().get(i).Name);
//                          textView13.setText(checkPointListEntity.getDevices().getDevPropertys().get(i).Value);
//                          TextView textView =new TextView(getContext());
//                          textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,0,1));
//                          textView.setText("测试");

                    ConstraintLayout layout = (ConstraintLayout) inflater.inflate(
                            R.layout.item_quest_detail_add_view, null).findViewById(R.id.cl);

                    ImageView imageView13 = (ImageView) layout.getViewById(R.id.imageView3);
                    if (historyQuestEntity.getQItemOptionList().get(i).getType()==1){
                        imageView13.setImageResource(R.drawable.date_d);
                    }else if (historyQuestEntity.getQItemOptionList().get(i).getType()==2){
                        imageView13.setImageResource(R.drawable.action_b);
                    }else{
                        imageView13.setImageResource(R.drawable.action_a);
                    }
                    TextView textView12 = (TextView) layout.getViewById(R.id.textView12);
                    TextView textView13 = (TextView) layout.getViewById(R.id.textView13);
                    textView12.setText(historyQuestEntity.getQItemOptionList().get(i).QOptionName + ":");
                    textView13.setText(historyQuestEntity.getQItemOptionList().get(i).InputValue);

                    llAddView.addView(layout);


                }
            }
        }


        if (historyQuestEntity.IsExistsMediaData==1){
            viewById1.setVisibility(View.VISIBLE);
        }else{
            viewById1.setVisibility(View.GONE);
        }
        viewById1.setOnClickListener(v->{
            //跳转到查看视频语音

            Intent intent =new Intent(getContext(), ShowMediaActivity.class);
            if (historyQuestEntity.getResultDesc()!=null){
                intent.putExtra("name",historyQuestEntity.getResultDesc());
                intent.putExtra("time",historyQuestEntity.getLastPatrolDateTime());

            }

            intent.putExtra("AlarmId",historyQuestEntity.getNo());
            getContext().startActivity(intent);



        });
        if (TextUtils.isEmpty(historyQuestEntity.getActionNo())){
            baseViewHolder.itemView.findViewById(R.id.imageView6).setVisibility(View.GONE);
            baseViewHolder.itemView.findViewById(R.id.textView14).setVisibility(View.GONE);
        }else{
            baseViewHolder.itemView.findViewById(R.id.imageView6).setVisibility(View.VISIBLE);
            baseViewHolder.itemView.findViewById(R.id.textView14).setVisibility(View.VISIBLE);
        }

        baseViewHolder.itemView.findViewById(R.id.cd_uncheck).setVisibility(View.GONE);
        baseViewHolder.setText(R.id.tv_upcoming_name,historyQuestEntity.getResultDesc());
        baseViewHolder.setText(R.id.tv_address,historyQuestEntity.getLastPatrolUser());
        baseViewHolder.setText(R.id.tv_to_address,historyQuestEntity.getLastPatrolDateTime());
        baseViewHolder.setText(R.id.tv_address2,historyQuestEntity.getActionNo());
        swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
            @Override
            public void onStartOpen(SwipeLayout layout) {
                baseViewHolder.itemView.findViewById(R.id.cl).setEnabled(false);
            }

            @Override
            public void onOpen(SwipeLayout layout) {
                baseViewHolder.itemView.findViewById(R.id.cl).setEnabled(false);
            }

            @Override
            public void onStartClose(SwipeLayout layout) {

            }

            @Override
            public void onClose(SwipeLayout layout) {
                baseViewHolder.itemView.findViewById(R.id.cl).setEnabled(true);
            }

            @Override
            public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {

            }

            @Override
            public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {

            }
        });


        baseViewHolder.itemView.findViewById(R.id.rl_Delete).setOnClickListener(v -> {
            new MaterialDialog.Builder(getContext())
                    .content("是否修復?")
                    .contentGravity(GravityEnum.CENTER)
                    .negativeColor(getContext().getResources().getColor(R.color.colorPrimary))
                    .positiveColor(getContext().getColor(R.color.colorPrimary))
                    .buttonsGravity(GravityEnum.CENTER)
                    .positiveText("是")
                    .negativeText("否")
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                            ActivityTool.getInstance().finishAllActivities();
//                          ARouter.getInstance().build("/Login/LoginActivity").navigation(getContext().getApplicationContext());

                            swipeLayout.close();

                            //修复
                            mDataCallBack.onDataSuccessCallBack(historyQuestEntity.getId());
                        }
                    })
                    .onNegative(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            swipeLayout.close();
                        }
                    })
                    .show();

        });

          if (TextUtils.isEmpty(historyQuestEntity.getLastPatrolUserPhone())){
              baseViewHolder.itemView.findViewById(R.id.iv_call).setVisibility(View.GONE);
          }else{
              baseViewHolder.itemView.findViewById(R.id.iv_call).setVisibility(View.VISIBLE);
          }

        baseViewHolder.itemView.findViewById(R.id.iv_call).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCallPhoneCallBack!=null){
                    onCallPhoneCallBack.onCallPhone(historyQuestEntity.getLastPatrolUserPhone());
                }
            }
        });
    }
    public void setmDataCallBack(MyCheckPointListCallBack<Integer> mDataCallBack) {
        this.mDataCallBack = mDataCallBack;
    }
}
